package com.beetle123.utils;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.common.utils.IOUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yuanlong.chen
 * @date 2021/09/07
 */
public class ParamParser {
    public static Map<String, String> parse(String appName) {
        String path = MyUtils.getBasePath() + "data/" + appName + ".data";
        Map<String, String> paramMap = new HashMap<>(16);
        File file = new File(path);
        if (!file.exists()) {
            return paramMap;
        }
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            String line;
            StringBuilder tpl = new StringBuilder();
            String key = null;
            while ((line = reader.readLine()) != null) {
                if (line.startsWith("//begin#")) {
                    key = line;
                } else if (line.startsWith("//end")) {
                    paramMap.put(key, tpl.toString());
                    tpl = new StringBuilder();
                    key = null;
                } else {
                    tpl.append(line);
                }
            }
            reader.close();
        } catch (IOException e) {
            LogUtils.println(MyUtils.getStackTrace(e));
            MyUtils.alert("解析参数模板异常");
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        LogUtils.println("解析参数模板结果:" + JSON.toJSONString(paramMap));
        return paramMap;
    }

    public static void save(String appName, Map<String, String> paramMap) {
        try {
            List<String> list = new ArrayList<>(16);
            for (Map.Entry<String, String> entry : paramMap.entrySet()) {
                list.add(entry.getKey());
                list.add(entry.getValue());
                list.add("//end");
            }
            String path = MyUtils.getBasePath() + "data/";
            File file = new File(path);
            if (!file.exists()) {
                file.mkdirs();
            }
            file = new File(path + appName + ".data");
            if (!file.exists()) {
                file.createNewFile();
            }
            IOUtils.writeLines(file, list.toArray(new String[list.size()]));
            MyUtils.alert("保存参数成功", 2);
        } catch (Exception e) {
            LogUtils.println(MyUtils.getStackTrace(e));
        }
    }

    public static String buildKey(String serviceUniqueKey, String method, String tplName) {
        String[] serviceInfos = StringUtils.split(serviceUniqueKey, ":");
        return String.format("//begin#%s#%s#%s", serviceInfos[0], method, tplName);
    }

    public static List<String> findNameList(String serviceUniqueKey, String method, Map<String, String> paramMap) {
        //serviceUniqueKey=tech.yummy.crm.member.facade.member.MemberActionFacade:1.0.0:dubbo:provider:member-service
        String[] serviceInfos = StringUtils.split(serviceUniqueKey, ":");
        String key = String.format("//begin#%s#%s#", serviceInfos[0], method);
        List<String> nameList = new ArrayList<>(16);
        for (Map.Entry<String, String> entry : paramMap.entrySet()) {
            if (entry.getKey().startsWith(key)) {
                nameList.add(entry.getKey().substring(key.length()));
            }
        }
        return nameList;
    }
}
